#include "BlurViewNode.h"
#include "RNOH/arkui/NativeNodeApi.h"
#include <cmath>
#include <cstdint>

namespace rnoh {
BlurViewNode::BlurViewNode() : ArkUINode(NativeNodeApi::getInstance()->createNode(ArkUI_NodeType::ARKUI_NODE_STACK)) {}
// 定义映射表：BlurType -> {style, mode}
static const std::unordered_map<facebook::react::BlurViewBlurType, std::pair<int32_t, int32_t>> blurTypeMap = {
    {facebook::react::BlurViewBlurType::DARK, {ARKUI_BLUR_STYLE_BACKGROUND_THICK, ARKUI_COLOR_MODE_DARK}},
    {facebook::react::BlurViewBlurType::LIGHT, {ARKUI_BLUR_STYLE_COMPONENT_THIN, ARKUI_COLOR_MODE_LIGHT}},
    {facebook::react::BlurViewBlurType::THICK_MATERIAL_DARK, {ARKUI_BLUR_STYLE_THICK, ARKUI_COLOR_MODE_DARK}},
    {facebook::react::BlurViewBlurType::THICK_MATERIAL_LIGHT, {ARKUI_BLUR_STYLE_THICK, ARKUI_COLOR_MODE_LIGHT}},
    {facebook::react::BlurViewBlurType::THIN_MATERIAL_LIGHT, {ARKUI_BLUR_STYLE_THIN, ARKUI_COLOR_MODE_LIGHT}},
    {facebook::react::BlurViewBlurType::THIN_MATERIAL_DARK, {ARKUI_BLUR_STYLE_THIN, ARKUI_COLOR_MODE_DARK}},
    {facebook::react::BlurViewBlurType::XLIGHT, {ARKUI_BLUR_STYLE_COMPONENT_THICK, ARKUI_COLOR_MODE_LIGHT}},
    {facebook::react::BlurViewBlurType::EXTRA_DARK, {ARKUI_BLUR_STYLE_COMPONENT_THICK, ARKUI_COLOR_MODE_DARK}},
    {facebook::react::BlurViewBlurType::REGULAR, {ARKUI_BLUR_STYLE_REGULAR, ARKUI_COLOR_MODE_SYSTEM}},
    {facebook::react::BlurViewBlurType::ULTRA_THIN_MATERIAL_DARK,
        {ARKUI_BLUR_STYLE_COMPONENT_ULTRA_THIN, ARKUI_COLOR_MODE_DARK}},
    {facebook::react::BlurViewBlurType::ULTRA_THIN_MATERIAL_LIGHT,
        {ARKUI_BLUR_STYLE_COMPONENT_ULTRA_THIN, ARKUI_COLOR_MODE_LIGHT}},
    {facebook::react::BlurViewBlurType::CHROME_MATERIAL_DARK,
        {ARKUI_BLUR_STYLE_COMPONENT_ULTRA_THICK, ARKUI_COLOR_MODE_DARK}},
    {facebook::react::BlurViewBlurType::MATERIAL_DARK, {ARKUI_BLUR_STYLE_COMPONENT_REGULAR, ARKUI_COLOR_MODE_DARK}},
    {facebook::react::BlurViewBlurType::MATERIAL_LIGHT,
        {ARKUI_BLUR_STYLE_COMPONENT_REGULAR, ARKUI_COLOR_MODE_LIGHT}},
    {facebook::react::BlurViewBlurType::CHROME_MATERIAL_LIGHT,
        {ARKUI_BLUR_STYLE_COMPONENT_ULTRA_THICK, ARKUI_COLOR_MODE_LIGHT}},
    {facebook::react::BlurViewBlurType::PROMINENT, {ARKUI_BLUR_STYLE_THICK, ARKUI_COLOR_MODE_SYSTEM}},
    {facebook::react::BlurViewBlurType::CHROME_MATERIAL,
        {ARKUI_BLUR_STYLE_COMPONENT_ULTRA_THICK, ARKUI_COLOR_MODE_SYSTEM}},
    {facebook::react::BlurViewBlurType::MATERIAL, {ARKUI_BLUR_STYLE_COMPONENT_REGULAR, ARKUI_COLOR_MODE_SYSTEM}},
    {facebook::react::BlurViewBlurType::THICK_MATERIAL, {ARKUI_BLUR_STYLE_COMPONENT_THICK, ARKUI_COLOR_MODE_SYSTEM}},
    {facebook::react::BlurViewBlurType::THIN_MATERIAL, {ARKUI_BLUR_STYLE_COMPONENT_THIN, ARKUI_COLOR_MODE_SYSTEM}},
    {facebook::react::BlurViewBlurType::ULTRA_THIN_MATERIAL,
        {ARKUI_BLUR_STYLE_COMPONENT_ULTRA_THIN, ARKUI_COLOR_MODE_SYSTEM}}
};
BlurViewNode &BlurViewNode::SetBlurType(facebook::react::BlurViewBlurType const &blurType, const int &blurAmount)
{
    // 查找映射
    int32_t style = ARKUI_BLUR_STYLE_THIN;
    int32_t mode = ARKUI_COLOR_MODE_DARK;
    const float mblurAmount = 100;
    auto it = blurTypeMap.find(blurType);
    if (it != blurTypeMap.end()) {
        style = it->second.first;
        mode = it->second.second;
    }
    DLOG(INFO) << "[BlurViewNode] style: " << std::int32_t(style);
    DLOG(INFO) << "[BlurViewNode] mode: " << std::int32_t(mode);
    DLOG(INFO) << "[BlurViewNode] blurAmount: " << (float(blurAmount) / mblurAmount);
    ArkUI_NumberValue BlurVal[] = {
        {.i32 = style}, {.i32 = mode}, {.i32 = ARKUI_ADAPTIVE_COLOR_DEFAULT}, {.f32 = float(blurAmount) / 100},
        {.f32 = 0},     {.f32 = 0}};
    ArkUI_AttributeItem BlurItem = {.value = BlurVal, .size = sizeof(BlurVal) / sizeof(ArkUI_NumberValue)};
    maybeThrow(NativeNodeApi::getInstance()->setAttribute(m_nodeHandle, NODE_FOREGROUND_BLUR_STYLE, &BlurItem));
    return *this;
}


void BlurViewNode::InsertChild(ArkUINode &child, std::size_t index)
{
    maybeThrow(NativeNodeApi::getInstance()->insertChildAt(m_nodeHandle, child.getArkUINodeHandle(), index));
};
void BlurViewNode::RemoveChild(ArkUINode &child)
{
    maybeThrow(NativeNodeApi::getInstance()->removeChild(m_nodeHandle, child.getArkUINodeHandle()));
};

} // namespace rnoh
